package com.dp.client.views.reportesCharts;

import com.google.gwt.i18n.client.*;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import org.moxieapps.gwt.highcharts.client.*;
import org.moxieapps.gwt.highcharts.client.labels.*;
import org.moxieapps.gwt.highcharts.client.plotOptions.*;

import com.google.gwt.i18n.client.NumberFormat;

public class CrearColumnRotateLabel{
	private Chart chart;
	private Point [] points;
	private String tipo;
	private String nombre;
	private String[] labels;
	private String msg;
	private String labelY;
	
	public CrearColumnRotateLabel(Point[] points,String nombre,String tipo,String msg,String labelY){
		this.points=points;
		this.nombre=nombre;
		this.tipo=tipo;
		this.msg=msg;
		this.labelY=labelY;
		labels= new String[points.length];
		for (int i=0;i<points.length;i++)
			labels[i]=points[i].getName();
		init();
	}
	
	public void init(){
		chart = new Chart()
        .setType(Series.Type.COLUMN)
        .setMargin(50, 50, 100, 80)
        .setChartTitleText(nombre)
        .setLegend(new Legend()
            .setEnabled(false)
        )
        .setToolTip(new ToolTip()
            .setFormatter(new ToolTipFormatter() {
                public String format(ToolTipData toolTipData) {
                	if (tipo.compareTo("double")==0)
                		return "<b>" + toolTipData.getXAsString() + "</b><br/>" +
                        msg +              NumberFormat.getFormat("0.0").format(toolTipData.getYAsDouble());
                	else
                		return "<b>" + toolTipData.getXAsString() + "</b><br/>" +
                        msg + toolTipData.getYAsLong();
                        
                }
            })
        );

    chart.getXAxis()
        .setCategories(
            labels
        )
        .setLabels(new XAxisLabels()
            .setRotation(-45)
            .setAlign(Labels.Align.RIGHT)
            .setStyle(new Style()
                .setFont("normal 13px Verdana, sans-serif")
            )
        );

    chart.getYAxis()
        .setAxisTitleText(labelY)
        .setMin(0);

    chart.addSeries(chart.createSeries()
        .setName("Population")
        .setPoints(points)
        .setPlotOptions(new ColumnPlotOptions()
            .setDataLabels(new DataLabels()
                .setEnabled(true)
                .setRotation(-90)
                .setColor("#FFFFFF")
                .setAlign(Labels.Align.RIGHT)
                .setX(-3)
                .setY(10)
                .setFormatter(new DataLabelsFormatter() {
                    public String format(DataLabelsData dataLabelsData) {
                    	if (tipo.compareTo("double")==0)
                    		return NumberFormat.getFormat("0.0").format(dataLabelsData.getYAsDouble())+"%";
                    	else
                    		return dataLabelsData.getYAsLong()+"";
                        
                    }
                })
                .setStyle(new Style()
                    .setFont("normal 13px Verdana, sans-serif")
                )
            )
        )
    );
	}
	
	public Chart getChart(){
		   
		return chart;
	}
}